<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="kissdata">


    <meta name="subtitle" content="人生笔记">


    <meta name="description" content="南邮计算机学院2021毕业生王鹏的介绍，cs软件工程专业">


    <meta name="keywords" content="wangpeng 王鹏 南邮 南京">


<title>开发环境 | Ubuntu 安装各种数据库 | 王鹏的小岛</title>



    <link rel="icon" href="/myIcon.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    



    
    
        <!-- MathJax配置，可通过单美元符号书写行内公式等 -->
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
    "HTML-CSS": {
        preferredFont: "TeX",
        availableFonts: ["STIX","TeX"],
        linebreaks: { automatic:true },
        EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50)
    },
    tex2jax: {
        inlineMath: [ ["$", "$"], ["\\(","\\)"] ],
        processEscapes: true,
        ignoreClass: "tex2jax_ignore|dno",
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    },
    TeX: {
        equationNumbers: { autoNumber: "AMS" },
        noUndefined: { attributes: { mathcolor: "red", mathbackground: "#FFEEEE", mathsize: "90%" } },
        Macros: { href: "{}" }
    },
    messageStyle: "none"
    });
</script>
<!-- 给MathJax元素添加has-jax class -->
<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>
<!-- 通过连接CDN加载MathJax的js代码 -->
<script type="text/javascript" async
        src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML">
</script>


    


<meta name="generator" content="Hexo 7.0.0"></head>

<body>
    <script>
        // this function is used to check current theme before page loaded.
        (() => {
            const currentTheme = window.localStorage && window.localStorage.getItem('theme') || '';
            const isDark = currentTheme === 'dark';
            const pagebody = document.getElementsByTagName('body')[0]
            if (isDark) {
                pagebody.classList.add('dark-theme');
                // mobile
                document.getElementById("mobile-toggle-theme").innerText = "· Dark"
            } else {
                pagebody.classList.remove('dark-theme');
                // mobile
                document.getElementById("mobile-toggle-theme").innerText = "· Light"
            }
        })();
    </script>

    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/">侠之大者</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/update/mystate">Career</a>
                
                    <a class="menu-item" href="/category">Legend</a>
                
                    <a class="menu-item" href="/about">Me</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">侠之大者</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/update/mystate">Career</a>
                
                    <a class="menu-item" href="/category">Legend</a>
                
                    <a class="menu-item" href="/about">Me</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
            <div class="main">
                <div class="container">
    
    
        <div class="post-toc">
    <div class="tocbot-list">
    </div>
    <div class="tocbot-list-menu">
        <a class="tocbot-toc-expand" onclick="expand_toc()">Expand all</a>
        <a onclick="go_top()">Back to top</a>
        <a onclick="go_bottom()">Go to bottom</a>
    </div>
</div>

<script>
    var tocbot_timer;
    var DEPTH_MAX = 6; // 为 6 时展开所有
    var tocbot_default_config = {
        tocSelector: '.tocbot-list',
        contentSelector: '.post-content',
        headingSelector: 'h1, h2, h3, h4, h5',
        orderedList: false,
        scrollSmooth: true,
        onClick: extend_click,
    };

    function extend_click() {
        clearTimeout(tocbot_timer);
        tocbot_timer = setTimeout(function() {
            tocbot.refresh(obj_merge(tocbot_default_config, {
                hasInnerContainers: true
            }));
        }, 420); // 这个值是由 tocbot 源码里定义的 scrollSmoothDuration 得来的
    }

    document.ready(function() {
        tocbot.init(obj_merge(tocbot_default_config, {
            collapseDepth: 1
        }));
    });

    function expand_toc() {
        var b = document.querySelector('.tocbot-toc-expand');
        var expanded = b.getAttribute('data-expanded');
        expanded ? b.removeAttribute('data-expanded') : b.setAttribute('data-expanded', true);
        tocbot.refresh(obj_merge(tocbot_default_config, {
            collapseDepth: expanded ? 1 : DEPTH_MAX
        }));
        b.innerText = expanded ? 'Expand all' : 'Collapse all';
    }

    function go_top() {
        window.scrollTo(0, 0);
    }

    function go_bottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }

    function obj_merge(target, source) {
        for (var item in source) {
            if (source.hasOwnProperty(item)) {
                target[item] = source[item];
            }
        }
        return target;
    }
</script>
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">开发环境 | Ubuntu 安装各种数据库</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/">kissdata</a>
                    

                    
                        <span class="post-time">
                        Date: <a href="#">Aug 18, 2023&nbsp;&nbsp;0:00</a>
                        </span>
                    
                    
                        <span class="post-category">
                    Category:
                            
                                <a href="/categories/Review/">Review</a>
                            
                        </span>
                    
                </div>
            
        </header>

        <div class="post-content">
            <h2 id="一、安装-MySQL8"><a href="#一、安装-MySQL8" class="headerlink" title="一、安装 MySQL8"></a>一、安装 MySQL8</h2><p>略，直接 docker 安装。</p>
<h2 id="二、安装-PG14"><a href="#二、安装-PG14" class="headerlink" title="二、安装 PG14"></a>二、安装 PG14</h2><blockquote>
<p>PG安装，会自动创建3个同名 <code>postgres</code> ，虽然名字都一样，却是不同方面的。一个是数据库用户，默认无密码；一个是数据库名字叫 <code>postgres</code>、还有一个是Linux用户 <code>postgres</code></p>
</blockquote>
<h3 id="2-1-安装"><a href="#2-1-安装" class="headerlink" title="2.1 安装"></a>2.1 安装</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-get update</span><br><span class="line">sudo apt-get install postgresql -y</span><br><span class="line">psql --version</span><br></pre></td></tr></table></figure>

<p>安装后不能直接 <code>psql</code> 使用，这样会提示 root 角色(role)不存在，需要切换到默认postgres账户才能使用。</p>
<p>【开启和关闭】默认是开启的</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sudo /etc/init.d/postgresql start</span><br><span class="line">sudo /etc/init.d/postgresql stop</span><br><span class="line">sudo /etc/init.d/postgresql restart</span><br></pre></td></tr></table></figure>

<h3 id="2-2-修改配置"><a href="#2-2-修改配置" class="headerlink" title="2.2 修改配置"></a>2.2 修改配置</h3><p>postgresql <strong>默认情况下除本机外的机器是不能连接的</strong>。所以ubuntu上安装好pg后，用win上的navicat连接不上。要想连接需要配置一下文件。</p>
<p>配置文件默认放在 &#x2F;etc&#x2F;postgresql&#x2F;14&#x2F;main&#x2F; 目录 ，不确定可用 <code>find / -name pg_hba.conf</code> 查找。只需要修改两个配置文件。</p>
<p>（1）在 <code>pg_hba.conf</code> 的末尾增加一行（或者加在ipv4下方）</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> /etc/postgresql/14/main/</span><br><span class="line">vim pg_hba.conf</span><br><span class="line"><span class="comment"># 在文件末尾ipv4下加一行</span></span><br><span class="line">host	all		all 0.0.0.0/0	trust</span><br></pre></td></tr></table></figure>

<p>参数 trust，如果换成 md5，表示要用加密口令才能连上pg。</p>
<p>（2）修改 <code>postgresql.conf</code> 的 4行</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> /etc/postgresql/14/main/</span><br><span class="line">vim postgresql.conf</span><br><span class="line">listen_addresses = <span class="string">&#x27;*&#x27;</span>           <span class="comment"># 换成星号</span></span><br><span class="line">port = 15432                     <span class="comment"># 和默认的5432区分。</span></span><br><span class="line">log_timezone = <span class="string">&#x27;posix/Asia/Shanghai&#x27;</span> </span><br><span class="line">timezone = <span class="string">&#x27;posix/Asia/Shanghai&#x27;</span> <span class="comment"># 时区调整</span></span><br></pre></td></tr></table></figure>

<p>其中 listen配置在Connection Settings下方，要先将前面的注释取消；时区配置默认是 <code>Etc/UTC</code>，不修改的话，后期即使Linux系统本身时区改对了（sh输入 <code>date</code> 检验），数据里转换的时候还是差8小时。</p>
<p>修改后 <code>sudo /etc/init.d/postgresql restart</code> 重启，Navicat16 就能连上了。</p>
<p><img src="/%5Cimage%5Cres_ubuntu%5Cnavicat_pg.png"></p>
<h3 id="2-3-测试"><a href="#2-3-测试" class="headerlink" title="2.3 测试"></a>2.3 测试</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">sudo passwd postgres  <span class="comment"># 修改linux用户 postgres的密码</span></span><br><span class="line"></span><br><span class="line">su postgres <span class="comment"># bash前缀变了</span></span><br><span class="line">psql</span><br><span class="line">show data_directory; <span class="comment"># pg语句, 带分号</span></span><br><span class="line">show time zone;</span><br><span class="line"><span class="keyword">select</span> now();        <span class="comment"># sql, 查询当前时间</span></span><br><span class="line">\q</span><br><span class="line"><span class="built_in">exit</span></span><br></pre></td></tr></table></figure>

<p>注意：先修改Linux账户 <code>postgres</code> 的密码（第一次用），修改后要切换到这个linux账户下，再psql使用pg。</p>
<h2 id="三、安装-mongodb6"><a href="#三、安装-mongodb6" class="headerlink" title="三、安装 mongodb6"></a>三、安装 mongodb6</h2><p>mongodb安装包的来源，可以在后面调数字。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2004-6.0.12.tgz</span><br><span class="line">https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2204-7.0.2.tgz</span><br></pre></td></tr></table></figure>

<p>【注】<code>v7.0</code> 以上不支持 ubuntu18，如果要装，可以用 docker 试试。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">tar -zxvf mongodb*2204* -C /usr/local</span><br><span class="line"><span class="built_in">cd</span> /usr/local</span><br><span class="line"><span class="built_in">mv</span> mongodb*2204* mongodb    <span class="comment"># 给解压出来的目录改名</span></span><br><span class="line"><span class="built_in">ls</span> mongodb/bin/</span><br></pre></td></tr></table></figure>

<h3 id="3-1-配置环境"><a href="#3-1-配置环境" class="headerlink" title="3.1 配置环境"></a>3.1 配置环境</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">vim /etc/profile</span><br><span class="line"><span class="built_in">export</span> PATH=<span class="variable">$PATH</span>:/usr/local/mongodb/bin    <span class="comment"># 增加这一行</span></span><br></pre></td></tr></table></figure>

<p>退出后</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">tail</span> -2 /etc/profile   <span class="comment"># 确认是否添加在文件最后</span></span><br><span class="line"><span class="built_in">source</span> /etc/profile</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$PATH</span>             <span class="comment"># 查看结果，是不是在末尾增加了一个路径</span></span><br></pre></td></tr></table></figure>

<p>重启。</p>
<blockquote>
<p>刚刚用 source 临时查看 PATH 的更新，bash关闭候就会失效。重启才会永久有效。</p>
</blockquote>
<h3 id="3-2-配置数据存储文件"><a href="#3-2-配置数据存储文件" class="headerlink" title="3.2 配置数据存储文件"></a>3.2 配置数据存储文件</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> /home/; <span class="built_in">touch</span> mongodb.conf; <span class="built_in">ls</span>   <span class="comment"># 在home目录新建 conf文件</span></span><br></pre></td></tr></table></figure>

<p>写入一下内容</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">dbpath=/data/mongodb_data # 指定数据库路径</span><br><span class="line">logpath=/data/mongodb_logs/mongodb.log # 指定日志文件</span><br><span class="line">logappend=true</span><br><span class="line"></span><br><span class="line">port=27017 </span><br><span class="line">bind_ip=0.0.0.0 # 外网所有ip都可以访问</span><br><span class="line"></span><br><span class="line">fork=true # 以守护进程的方式运行MongoDB，创建服务器进程</span><br><span class="line">auth=false</span><br></pre></td></tr></table></figure>

<p>按照前两行的配置值，在对应的位置创建两个目录（这里是在根目录再创建一个 data目录）。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">sudo <span class="built_in">mkdir</span> -p /data/mongdb_data /data/mongodb_logs</span><br><span class="line">sudo <span class="built_in">touch</span> /data/mongodb_logs/mongodb.log</span><br><span class="line"><span class="built_in">chmod</span> -R 777 /data/</span><br><span class="line"><span class="built_in">chmod</span> -R 777 /data/mongodb_data /data/mongodb_logs</span><br></pre></td></tr></table></figure>

<h3 id="3-3-测试"><a href="#3-3-测试" class="headerlink" title="3.3 测试"></a>3.3 测试</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">mongod --config /home/mongodb.conf        <span class="comment"># 开启</span></span><br><span class="line">ps -ef [m]ongo                            <span class="comment"># 检查状态</span></span><br><span class="line">mongod --shutdown -f /home/mongodb.conf   <span class="comment"># 关闭</span></span><br></pre></td></tr></table></figure>

<p>参数 <code>-f</code> 和 <code>--config</code> 等效</p>
<p><img src="/%5Cimage%5Cres_ubuntu%5Cmongodb_test.png"></p>

        </div>

        
            <section class="post-copyright">
                
                
                
                
                     <p class="copyright-item">
                         <span>Slogan:</span>
                         <span>人生三大错觉：我躲得过去，我能反杀，是不是<strong>喜欢我?</strong></span>
                     </p>
                

            </section>
        
        <section class="post-tags">
            <div>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/2023/09-12/propaganda_celebrity/">圣人出</a>
            
            
            <a class="next" rel="next" href="/2022/12-24/review_stringtail/">\0 的魔法</a>
            
        </section>


    </article>
</div>

            </div>
            <footer id="footer" class="footer">
    <div class="copyright">
        <span>187-9597-5517 &nbsp; 王鹏 @ <a href="https://cs.njupt.edu.cn/" target="_blank">njupt</a</span>
    </div>
</footer>

    </div>
</body>

</html>